package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareUserfansBinding;
import com.sgb.knowledgeshare.entity.FollowListEntity;
import com.sgb.knowledgeshare.view.adapter.ShareUserFansAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareUserFansModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.List;



/**
 * Created by fq on 2020/9/10 0010
 * Description：分享-用户中心-粉丝
 */
public class ShareUserFansActivity extends AppActivity {

    ShareUserFansModel viewModel;
    private static final String USERID= "userid";
    private ActivityShareUserfansBinding binding;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private ShareUserFansAdapter shareuserfansadapter;
    private List<FollowListEntity.RecordsBean> follow;
    private boolean isLoadMore;
    private int page;

    public static void start(Context context,String userid) {
        Intent intent = new Intent();
        intent.putExtra(USERID, userid);
        intent.setClass(context, ShareUserFansActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_userfans);
        viewModel = new ViewModelProvider(this).get(ShareUserFansModel.class);
        viewModel.setActivity(ShareUserFansActivity.this);
        View decor =getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.include.titleView.setText("TA的粉丝");
        binding.include.ivBack.setOnClickListener(this);
        shareuserfansadapter = new ShareUserFansAdapter(getActivity(), null);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(shareuserfansadapter);
        binding.lrecyclerview.setAdapter(mLRecyclerViewAdapter);
        binding.lrecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //点击用户信息跳转到对应的用户详情界面
                ShareUserActivity.start(getApplicationContext(),follow.get(position).getUserNo());
            }
        });
        binding.lrecyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                shareuserfansadapter.getDatas().clear();
                isLoadMore=false;
                initdata();
                mLRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
        binding.lrecyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore=true;
                page++;
                viewModel.getFensList(page,getIntent().getStringExtra(USERID));
            }
        });
        binding.lrecyclerview.refresh();
       shareuserfansadapter.setFensData(new ShareUserFansAdapter.FensData() {
           @Override
           public void fens(String UserNo) {
               if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "").equals(UserNo)) {
                   viewModel.getFens(UserNo);
               } else {
                   MToast.showToast(getApplicationContext(), "自己不能关注自己");
               }
           }
       });
    }

    @Override
    protected void init() {
        initdata();
    }

    private void initdata() {
        isLoadMore=false;
        page = 1;
        viewModel.getFensList(page,getIntent().getStringExtra(USERID));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {//返回
            finish();
        }
    }

    public void setFensList(FollowListEntity data) {
        if (null==data){
            return;
        }
        this.follow = data.getRecords();
        binding.lrecyclerview.refreshComplete(page);
        binding.lrecyclerview.setPullRefreshEnabled(true);
        if (isLoadMore) {
            if (data.getRecords().size() > 0) {
                shareuserfansadapter.addDatas(data.getRecords());
            } else {
                MToast.showToast(this, "没有视频啦");
            }
        } else {
            if (data.getRecords().size() > 0) {
                binding.lrecyclerview.setVisibility(View.VISIBLE);
                binding.empty.setVisibility(View.GONE);
                shareuserfansadapter.setDatas(data.getRecords());
            } else {
                shareuserfansadapter.setDatas(null);
                binding.lrecyclerview.setVisibility(View.GONE);
                binding.empty.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setFens() {
        initdata();
    }
}
